Skip to main content

Enumeration

An enumeration is a user-defined data type composed of a list of comma-separated members (enumeration values) for declaring user-defined variables.

You declare an enumeration in a DUT object, which you have already created in the project by clicking Add Object.

In the application code, you can use the enumeration members like constants whose identifier <enumeration name>.<member name> is recognized globally in the project.

For more information, see: DUT

Declaration

Syntax

{attribute 'strict'}

TYPE <enumeration name> :

(

    <member name> := <value>

) <base data type> := <initialization> ;

END_TYPE

{attribute 'strict'}

Optional

The pragma has the effect that a strict type test to be performed as described below.

The pragma is optional, but recommended.

<enumeration name>

Name of the enumeration which can be used in the code as a data type

Example: COLOR_BASIC

    <member name> := <initial value>

Any number of members, but at least twp members

An enumeration is a comma-separated list of member names in round brackets. The last member before the closing round bracket does not need a comma. All members have the same data type.

The values of the members are automatically initialized: Starting at 0, the values are continuously incremented by 1. You can also explicitly assign a fixed initial value to the individual members.

Example: yellow := 1

<base data type>

Optional

You can explicitly assign one of the following base data types:

INT | UINT | SINT | USINT | DINT | UDINT | LINT | ULINT | BYTE | WORD | DWORD | LWORD

Default: INT

:= <initialization>

Optional

One of the members can be explicitly declared as initial members.

Default: If an initialization is not explicitly specified, then initialization is automatically performed with the top member.

Example 227. Example
{attribute 'qualified_only'}
{attribute 'strict'}
TYPE COLOR_BASIC :
(
    yellow,
    green,
    blue,
    black
) // Basic data type is INT, default initialization for all COLOR_BASIC variables is yellow
;
END_TYPE


Enumeration with explicit base data type

Extensions of the IEC 61131-3 standard

The base data type for an enumeration declaration is INT by default. However, you can also declare enumerations that are based explicitly on another integer data type.

Example 228. Example

Enumeration with base data type DWORD

TYPE COLOR :
(
        white := 16#FFFFFF00,
        yellow := 16#FFFFFF00,
        green := 16#FF00FF00,
        blue := 16#FF0000FF,
        black := 16#88000000
) DWORD := black
; // Basic data type is DWORD, default initialization for all COLOR variables is black
END_TYPE


Strict programming rules

Important

In CODESYS V3.5 SP7 and higher, the pragma {attribute 'strict'} is added automatically in the first line when declaring an enumeration.

The strict programming rules are activated when adding the pragma {attribute 'strict'}.

. The following code is considered a compiler error:
  • Arithmetic operations with enumeration members

    For example, an enumeration variable cannot be used as a counter variable in a FOR loop.

  • Assignment of a constant value, which does not correspond to an enumeration value, to an enumeration member

  • Assignment of a non-constant variable, which has another data type as the enumeration, to an enumeration member

Arithmetic operations can lead to undeclared values being assigned to enumeration members. A better programming style is to use SWITCH/CASE statements for processing member values.

Declaration and initialization of enumeration variables

Syntax

<variable name> : <enumeration name> := <initialization> ;

In the case of a declaration of an enumeration variable with user-defined enumeration, the variable can be initialized with an enumeration member.

Example 229. Example
PROGRAM PLC_PRG
VAR
    colorCar: COLOR;
    colorTaxi : COLOR := COLOR.yellow;
END_VAR

The variable colorCar is initialized with COLOR.black. That is the default initialization for all enumeration variables of type COLOR and defined this way in the type declaration. The variable colorTaxi has its own initialization.



If no initializations are specified, then the initialization value is 0.

Example 230. Example
PROGRAM PLC_PRG
VAR
    cbFlower : COLOR_BASIC;
    cbTree: COLOR_BASIC := COLOR_BASIC.green;
END_VAR

The variable cbFlower is initialized with COLOR_BASIC.yellow. That is the default initialization for all enumeration variables of type COLOR_BASIC. Because the enumeration declaration does not specify a member for initialization, the system automatically initializes with the member that has the value 0. This is usually the first of the enumeration members. However, it can also be another member that is not in the first position but explicitly initialized with 0.

The variable cbTree has an explicit initialization.



If no value is specified for both the type and the variable, then the following rule applies: If an enumeration contains a value for 0, then this value is the default initialization, and if not, then the first member in the list.

Example 231. Example

Initialization with the 0 member

TYPE ENUM :
(
    e1 := 2,
    e2 := 0,
    e3
)
;
END_TYPE

PROGRAM PLC_PRG
VAR
    e : ENUM;
END_VAR

The variable e is initialized with ENUM.e2.

Initialization with the first member

TYPE ENUM2 :
(
    e1 := 3,
    e2 := 1,
    e3
)
;
END_TYPE

PROGRAM PLC_PRG
VAR
    e2 : ENUM2;
END_VAR

The variable e2 is initialized with ENUM.e1.



Unique access to enumeration members

Extensions of the IEC 61131-3 standard

The enumeration members can also be used as constant variables with the identifier <enumeration name>.<member name>. Enumeration members are recognized globally in the project and access to them is unique. Therefore, a member name can be used in different enumerations.

Example 232. Example

Member: blue

PROGRAM PLC_PRG
VAR
    cbFlower : COLOR_BASIC;
    colorCar : COLOR;
END_VAR

(* unambiguous identifiers although the component names are identical *)
cbFlower := COLOR_BASIC.blue;
colorCar := COLOR.blue;

(* invalid code *)
cbFlower := blue;
colorCar := blue;


For more information, see: Enumeration Namespace.